<?php

namespace api\modules\v1\forms\base;

use Yii;
use yii\base\Model;
use common\models\base\BbBodyParts;


class BbBodyPartsForm extends Model
{
    // 部位ID
    public $id;
    // 父部位ID
    public $pid;
    // 部位名称
    public $body_parts_name;
    // 排序
    public $sort;
    // 状态
    public $status;


    // 部位信息
    protected $_body;

    public function rules()
    {
        return [
            // 参数验证
            [['body_parts_name', 'sort', 'pid'], 'required', 'message' => '{attribute}不能为空', 'on' => ['create']],
            [['id', 'body_parts_name', 'sort', 'pid', 'status'], 'required', 'message' => '{attribute}不能为空', 'on' => ['edit']],
            [['id', 'status'], 'required', 'message' => '{attribute}不能为空', 'on' => ['edit_status']],


            [['id'], 'checkId', 'skipOnEmpty' => false, 'on' => ['edit', 'edit_status']],
            [['body_parts_name'], 'checkBodyPartsName', 'skipOnEmpty' => true, 'on' => ['create', 'edit']],
            [['sort'], 'checkSort', 'skipOnEmpty' => true, 'on' => ['create', 'edit']],
            [['pid'], 'checkPid', 'skipOnEmpty' => true, 'on' => ['create', 'edit']],
            [['status'], 'checkStatus', 'skipOnEmpty' => false, 'on' => ['edit', 'edit_status']],
        ];
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();

        // 新增
        $scenarios['create'] = ['body_parts_name', 'sort', 'pid'];

        // 编辑
        $scenarios['edit'] = ['id', 'body_parts_name', 'sort', 'pid', 'status'];

        // 删除
        // $scenarios['delete'] = ['id'];

        // 编辑状态
        $scenarios['edit_status'] = ['id', 'status'];

        return $scenarios;
    }

    /**
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'member_id' => '用户ID',
            'id' => '部位ID',
            'pid' => '父部位ID',
            'body_parts_name' => '部位名称',
            'sort' => '排序',
            'status' => '状态',
        ];
    }

    /**
     * 检验部位ID
     *
     * @param $attribute
     * @return bool
     */
    public function checkId($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        $body = $this->getBody();
        if (empty($body)) {
            $this->addError($attribute, '部位不存在');
            return true;
        }

        if ($body->status == BbBodyParts::STATUS_DELETE) {
            $this->addError($attribute, '部位已删除');
            return true;
        }
    }

    /**
     * 校验部位名称
     *
     * @param $attribute
     */
    public function checkBodyPartsName($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }


        $len = mb_strlen($this->body_parts_name);
        if ($len > 50) {
            $this->addError($attribute, '部位名称过长，最多50个字');
            return true;
        }

        // 是否检验
        if ($this->pid == 0) {
            $isCheck = true;
            if ($this->getScenario() == 'edit') {
                $body = $this->getBody();
                // 部位参数未改变【跳过】
                if ($body->body_parts_name == $this->body_parts_name) {
                    $isCheck = false;
                }
            }
        } else {
            $isCheck = false;
        }

        if ($isCheck) {
            $bbBodyParts = BbBodyParts::findOne([
                'pid' => 0,
                'body_parts_name' => $this->body_parts_name,
                'status' => [BbBodyParts::STATUS_INVALID, BbBodyParts::STATUS_ENABLE],
            ]);
            if ($bbBodyParts) {
                $this->addError($attribute, "部位【{$this->body_parts_name}】已存在");
                return true;
            }
        }
    }

    /**
     * 校验菜单排序
     *
     * @param $attribute
     */
    public function checkSort($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        if ($this->sort < 0) {
            $this->addError($attribute, '排序值不能小于0');
            return true;
        }
    }

    /**
     * 校验父部位ID
     *
     * @param $attribute
     */
    public function checkPid($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        if ($this->pid == 0) {
            return true;
        }

        $pBody = BbBodyParts::findOne($this->pid);
        if (empty($pBody)) {
            $this->addError($attribute, '父部位不存在');
            return true;
        }

        if ($pBody->status == BbBodyParts::STATUS_DELETE) {
            $this->addError($attribute, '父部位已删除');
            return true;
        }

        if ($pBody->pid != 0) {
            $this->addError($attribute, '父部位非一级部位');
            return true;
        }
    }

    /**
     * 校验菜单状态
     *
     * @param $attribute
     */
    public function checkStatus($attribute)
    {
        if ($this->hasErrors()) {
            return true;
        }

        if (!in_array($this->status, [BbBodyParts::STATUS_INVALID, BbBodyParts::STATUS_ENABLE])) {
            $this->addError($attribute, "非法状态");
            return true;
        }
    }

    /**
     * 获取部位信息
     *
     * @return BbBodyParts|null
     */
    public function getBody(): ?BbBodyParts
    {
        if (empty($this->_body)) {
            $this->_body = BbBodyParts::findOne($this->id);
        }

        return $this->_body;
    }
}
